package com.xbai.user.controller;

import com.github.pagehelper.PageInfo;
import com.xbai.entity.Result;
import com.xbai.user.pojo.Areas;
import com.xbai.user.service.AreasService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @desc AreasController
 * @author jxbai
 * @date 2021/1/28
 */

@RestController
@RequestMapping("/areas")
@CrossOrigin
@RequiredArgsConstructor
public class AreasController {

    private final AreasService areasService;

    /***
     * Areas分页条件搜索实现
     * @param areas {@link Areas}
     * @param page 页码
     * @param size 页大小
     * @return {@link Result<PageInfo>}
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) Areas areas, @PathVariable  int page, @PathVariable  int size){
        //调用AreasService实现分页条件查询Areas
        PageInfo<Areas> pageInfo = areasService.findPage(areas, page, size);
        return new Result<>(pageInfo);
    }

    /***
     * Areas分页搜索实现
     * @param page 页码
     * @param size 页大小
     * @return {@link Result<PageInfo>}
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用AreasService实现分页查询Areas
        PageInfo<Areas> pageInfo = areasService.findPage(page, size);
        return new Result<>(pageInfo);
    }

    /***
     * 多条件搜索Areas数据
     * @param areas {@link Areas}
     * @return {@link Result}
     */
    @PostMapping(value = "/search" )
    public Result<List<Areas>> findList(@RequestBody(required = false) Areas areas){
        //调用AreasService实现条件查询Areas
        List<Areas> list = areasService.findList(areas);
        return new Result<>(list);
    }

    /***
     * 根据ID删除Areas数据
     * @param id 主键id
     * @return {@link Result}
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable String id){
        //调用AreasService实现根据主键删除
        areasService.delete(id);
        return new Result();
    }

    /***
     * 修改Areas数据
     * @param areas {@link Areas}
     * @param id 主键id
     * @return {@link Result}
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody Areas areas, @PathVariable String id){
        //设置主键值
        areas.setAreaid(id);
        //调用AreasService实现修改Areas
        areasService.update(areas);
        return new Result();
    }

    /***
     * 新增Areas数据
     * @param areas {@link Areas}
     * @return {@link Result}
     */
    @PostMapping
    public Result add(@RequestBody Areas areas){
        //调用AreasService实现添加Areas
        areasService.add(areas);
        return new Result();
    }

    /***
     * 根据ID查询Areas数据
     * @param id 主键id
     * @return {@link Result}
     */
    @GetMapping("/{id}")
    public Result<Areas> findById(@PathVariable String id){
        //调用AreasService实现根据主键查询Areas
        Areas areas = areasService.findById(id);
        return new Result<>(areas);
    }

    /***
     * 查询Areas全部数据
     * @return {@link Result}
     */
    @GetMapping
    public Result<List<Areas>> findAll(){
        //调用AreasService实现查询所有Areas
        List<Areas> list = areasService.findAll();
        return new Result<>(list) ;
    }
}
